package net.shopxx.action.shop;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import net.shopxx.bean.Pager;
import net.shopxx.bean.SystemConfig;
import net.shopxx.entity.DeliveryCorp;
import net.shopxx.entity.Member;
import net.shopxx.entity.Receiver;
import net.shopxx.entity.Refund;
import net.shopxx.entity.Refundnote;
import net.shopxx.entity.Refundproof;
import net.shopxx.service.DeliveryCorpService;
import net.shopxx.service.MemberService;
import net.shopxx.service.OrderService;
import net.shopxx.service.ReceiverService;
import net.shopxx.service.RefundService;
import net.shopxx.util.CommonUtil;
import net.shopxx.util.ImageUtil;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.ParentPackage;

/**
 * 后台Action类 - 发货
 */

@ParentPackage("shop")
public class RefundAction extends BaseShopAction {

	private static final long serialVersionUID = 3763457558863574061L;

	private Refund refund;

	private Member saler;
	
	private String processReason;

	private File[] refundProof;

	private String processStatus;

	private Refundnote refundnote;

	@Resource
	private RefundService refundService;
	@Resource
	private ReceiverService receiverService;
	@Resource
	private MemberService memberService;
	@Resource
	private DeliveryCorpService deliveryCorpService;
	@Resource
	private OrderService orderService;

	// 查看
	public String view() {
		refund = refundService.load(id);
		for (Refundproof proof : refund.getRefundproofSet()) {
			System.out.println(proof.getProofUrl());
		}
		return VIEW;
	}

	public String add() {
		return "add";
	}

	public String savenote() {
		Refund entity = refundService.load(id);
		refundnote.setRefund(entity);
		refundnote.setMember(getLoginMember());
		entity.getRefundnoteSet().add(refundnote);
		refundService.update(entity);
		redirectionUrl = "refund!view.action?id="+entity.getId();
		try {
			getResponse().sendRedirect(redirectionUrl);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public String saveShipping() {
		Refund entity = refundService.load(id);
		entity.setDeliveryCorpName(refund.getDeliveryCorpName());
		entity.setShippingNo(refund.getShippingNo());
		entity.setShippingStatus(Refund.ShippingStatus.shipped);
		refundService.update(entity);
		refund = entity;
		redirectionUrl = "refund!view.action?id="+refund.getId();
		try {
			getResponse().sendRedirect(redirectionUrl);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	// 查看
	public String processSave() {
		Refund entity = refundService.load(id);
		entity.setRefundStatus(Refund.RefundStatus.platformed);
		entity.setProcessStatus(Enum.valueOf(Refund.ProcessStatus.class,
				processStatus));
		refundService.update(entity);
		redirectionUrl = "refund!view.action?id="+entity.getId();
		try {
			getResponse().sendRedirect(redirectionUrl);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public String proofSave() {
		refund = refundService.load(id);
		Set<Refundproof> refundproofSet = new HashSet<Refundproof>();
		if (refundProof != null && refundProof.length > 0) {
			for (int i = 0; i < refundProof.length; i++) {
				if (refundProof[i] == null) {
					continue;
				}
				String sourceProductImageFormatName = ImageUtil
						.getImageFormatName(refundProof[i]);
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
						"yyyyMM");
				String dateString = simpleDateFormat.format(new Date());
				String uuid = CommonUtil.getUUID();
				String sourceProductImagePath = SystemConfig.UPLOAD_FILE_DIR
						+ dateString + "/" + uuid + "."
						+ sourceProductImageFormatName;

				File sourceProductImageFile = new File(ServletActionContext
						.getServletContext()
						.getRealPath(sourceProductImagePath));
				File sourceProductImageParentFile = sourceProductImageFile
						.getParentFile();

				if (!sourceProductImageParentFile.exists()) {
					sourceProductImageParentFile.mkdirs();
				}

				try {
					// 将上传图片复制到原图片目录
					FileUtils.copyFile(refundProof[i], sourceProductImageFile);
				} catch (IOException e) {
					e.printStackTrace();
				}
				Refundproof proof = new Refundproof();
				proof.setProofUrl(sourceProductImagePath);
				proof.setMember(getLoginMember());
				proof.setRefund(refund);
				refundproofSet.add(proof);
			}
		}
		refund.getRefundproofSet().addAll(refundproofSet);
		refundService.update(refund);
		redirectionUrl = "refund!view.action?id="+refund.getId();
		try {
			getResponse().sendRedirect(redirectionUrl);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public String edit() {
		refund = refundService.load(id);
		return "edit";
	}

	public String save() {
		refund.setRefundStatus(Refund.RefundStatus.requested);
		if(refund.getGoodsStatus().equals(Refund.GoodsStatus.noRecevied))
		{
			refund.setShippingStatus(Refund.ShippingStatus.unshipped);
		}
		else
		{
			refund.setShippingStatus(Refund.ShippingStatus.unshipped);
		}
		Set<Refundproof> refundproofSet = new HashSet<Refundproof>();
		if (refundProof != null && refundProof.length > 0) {
			for (int i = 0; i < refundProof.length; i++) {
				if (refundProof[i] == null) {
					continue;
				}

				String sourceProductImageFormatName = ImageUtil
						.getImageFormatName(refundProof[i]);
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
						"yyyyMM");
				String dateString = simpleDateFormat.format(new Date());
				String uuid = CommonUtil.getUUID();

				String sourceProductImagePath = SystemConfig.UPLOAD_FILE_DIR
						+ dateString + "/" + uuid + "."
						+ sourceProductImageFormatName;

				File sourceProductImageFile = new File(ServletActionContext
						.getServletContext()
						.getRealPath(sourceProductImagePath));
				File sourceProductImageParentFile = sourceProductImageFile
						.getParentFile();

				if (!sourceProductImageParentFile.exists()) {
					sourceProductImageParentFile.mkdirs();
				}
				try {
					// 将上传图片复制到原图片目录
					FileUtils.copyFile(refundProof[i], sourceProductImageFile);
				} catch (IOException e) {
					e.printStackTrace();
				}
				Refundproof proof = new Refundproof();
				proof.setProofUrl(sourceProductImagePath);
				proof.setMember(getLoginMember());
				proof.setRefund(refund);
				refundproofSet.add(proof);
			}
		}
		refund.setRefundproofSet(refundproofSet);
		refundService.save(refund);
		redirectionUrl = "order!list.action";
		try {
			getResponse().sendRedirect(redirectionUrl);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public String update() {
		Refund entity = refundService.load(id);
		entity.setProcessStatus(Refund.ProcessStatus.platform_joined);
		refundService.update(entity);
		
		redirectionUrl = "refund!view.action?id=" +entity.getId();
		try {
			getResponse().sendRedirect(redirectionUrl);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
		
	}

	// 列表
	public String list() {
		if (pager == null)
			pager = new Pager();
		pager.getWhereClause().put("order.member.id", getLoginMember()==null?null:getLoginMember().getId());
		pager = refundService.findByPager(pager);
		getRequest().setAttribute("refundAction", this);
		return LIST;
	}

	// 删除
	public String delete() {
		try {
			refundService.delete(ids);
		} catch (Exception e) {
			return ajaxJsonErrorMessage("删除失败，会员数据被引用！");
		}
		return ajaxJsonSuccessMessage("删除成功！");
	}

	public Refund getRefund() {
		return refund;
	}

	public void setRefund(Refund refund) {
		this.refund = refund;
	}

	public Member getSaler() {
		return saler;
	}

	public Member getSaler(String id) {
		return memberService.load(id);
	}

	public void setSaler(Member saler) {
		this.saler = saler;
	}

	public File[] getRefundProof() {
		return refundProof;
	}

	public void setRefundProof(File[] refundProof) {
		this.refundProof = refundProof;
	}

	// 获取所有物流公司
	public List<DeliveryCorp> getAllDeliveryCorp() {
		return deliveryCorpService.getAll();
	}
	
	public List<Receiver> getAllReceiver()
	{
		return receiverService.getAll();
	}

	public String getProcessStatus() {
		return processStatus;
	}

	public Refundnote getRefundnote() {
		return refundnote;
	}

	public void setProcessStatus(String processStatus) {
		this.processStatus = processStatus;
	}

	public void setRefundnote(Refundnote refundnote) {
		this.refundnote = refundnote;
	}

	public String getProcessReason() {
		return processReason;
	}

	public void setProcessReason(String processReason) {
		this.processReason = processReason;
	}
	
	

}